﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace NXBSaiGon
{
    [Serializable]
    public class Basket
    {
        public int total = 0;
        public int quantity = 0;

        private DataTable data;

        public Basket()
        {
            data = new DataTable();

            data.Columns.Add("STT", typeof(int));
            data.Columns.Add("ID", typeof(string));
            data.Columns.Add("DisplayName", typeof(string));
            data.Columns.Add("NgayXuatBan", typeof(DateTime));
            data.Columns.Add("MoTa", typeof(string));
            data.Columns.Add("Quantity", typeof(int));
            data.Columns.Add("Price", typeof(long));
            data.Columns.Add("ImgUrl", typeof(string));
            data.Columns.Add("ChieuCao", typeof(int));
            data.Columns.Add("ChieuRong", typeof(int));
            data.Columns.Add("BeDay", typeof(int));
            data.Columns.Add("KhoiLuong", typeof(int));
            data.PrimaryKey = new DataColumn[] { this.data.Columns["ID"] };
        }

        #region Method
        public bool isExits(string id)
        {
            foreach (DataRow dong in data.Rows)
            {
                if (dong["ID"].ToString() == id)
                    return true;
            }
            return false;

        }
        public bool Add(string id, string name, DateTime ngayXuatBan, string moTa, string imgUrl, 
                    int chieuCao, int chieuRong, int beDay, int khoiLuong, int quant, long price)
        {
            try
            {
                if (isExits(id))
                {
                    DataRow dong = data.Rows.Find(id);
                    if (dong != null)
                    {
                        dong["Quantity"] = (int)dong["Quantity"] + quant;
                        this.quantity += quant;
                        this.total += quant * (int)price;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    DataRow newrow = this.data.NewRow();
                    newrow["STT"] = this.quantity + 1;
                    newrow["ID"] = id;
                    newrow["DisplayName"] = name;
                    newrow["Quantity"] = quant;
                    newrow["Price"] = price;
                    newrow["NgayXuatBan"] = ngayXuatBan;
                    newrow["MoTa"] = moTa;
                    newrow["ImgUrl"] = imgUrl;
                    newrow["ChieuCao"] = chieuCao;
                    newrow["ChieuRong"] = chieuRong;
                    newrow["BeDay"] = beDay;
                    newrow["KhoiLuong"] = khoiLuong;
                    this.data.Rows.Add(newrow);
                    this.quantity += quant;
                    this.total += quant * (int)price;
                    //this.quantity++;
                }
                return true;
            }
            catch { return false; }
        }
        public bool Remove(string id)
        {
            DataRow dong = data.Rows.Find(id);
            if (dong != null)
            {
                this.total -= Convert.ToInt32(dong["Quantity"]) * Convert.ToInt32(dong["Price"]);
                this.quantity -= (int)dong["Quantity"];
                dong.Delete();
                data.AcceptChanges();
                return true;
            }
            return false;
        }
        private void updateStt()
        {
            int dem = 1;
            foreach (DataRow dong in data.Rows)
            {
                dong["STT"] = dem;
                dem++;
            }
        }
        private int GetTotal()
        {
            this.total = 0;
            foreach (DataRow dong in this.data.Rows)
            {
                this.total = this.total + (int)dong["Price"] * (int)dong["Quantity"];
            }
            return this.total;
        }

        public bool UpdateQuantity(string id, int quan)
        {
            DataRow dong = this.data.Rows.Find(id);
            if (dong != null)
            {
                dong[5] = quan;
                this.data.AcceptChanges();
                return true;
            }
            return false;
        }
        public bool Clear()
        {
            this.total = 0;
            this.quantity = 0;
            this.data.Clear();
            return true;
        }

        public DataTable GetProduct(string id)
        {
            DataTable pnew = this.Data.Clone();
            pnew.Clear();
            DataRow dong = this.data.Rows.Find(id);
            if (dong != null)
            {
                DataRow newrow = pnew.NewRow();
                newrow["STT"] = "1";
                newrow["ID"] = dong["ID"].ToString();
                newrow["DisplayName"] = dong["DisplayName"].ToString();
                newrow["Quantity"] = dong["Quantity"].ToString();
                newrow["Price"] = dong["Price"].ToString();
                newrow["NgayXuatBan"] = dong["NgayXuatBan"].ToString();
                newrow["MoTa"] = dong["MoTa"].ToString();
                newrow["ImgUrl"] = dong["ImgUrl"].ToString();
                newrow["ChieuCao"] = dong["ChieuCao"].ToString();
                newrow["ChieuRong"] = dong["ChieuRong"].ToString();
                newrow["BeDay"] = dong["BeDay"].ToString();
                newrow["KhoiLuong"] = dong["KhoiLuong"].ToString();
                if (!pnew.Columns.Contains("Total"))
                {
                    pnew.Columns.Add("Total", typeof(Int64));
                }
                newrow["Total"] = (long)newrow["Price"] * (int)newrow["Quantity"];
                pnew.Rows.Add(newrow);
            }

            return pnew;
        }

        public string getTransportString()
        {
            string TranString = "";
            int count = data.Rows.Count;
            DataRow r = null;
            for (int i = 0; i < count; ++i)
            {
                r = data.Rows[i];
                if (i == count - 1)
                {
                    TranString = TranString + (string)r["DisplayName"] + ": " + r["Quantity"];
                }
                else
                {
                    TranString = TranString + (string)r["DisplayName"] + ": " + r["Quantity"] + " | ";
                }
            }
            return TranString;
        }

        #endregion

        #region Properties

        public int Total
        {
            get { return this.total; }
        }
        public int Quantity
        {
            get { return this.quantity; }
        }

        public DataTable Data
        {
            get
            {
                updateStt();
                if (!data.Columns.Contains("Total"))
                {
                    data.Columns.Add("Total", typeof(Int64));
                }
                foreach (DataRow dong in this.data.Rows)
                {
                    dong["Total"] = (long)dong["Price"] * (int)dong["Quantity"];
                }
                return this.data;
            }
        }

        #endregion

    }
}